// pages/user/userCard/userCard.js
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    titleArr: [{ 'tit': '标语：' }, { 'tit': '姓名：' }, { 'tit': '电话：' }, { 'tit': '店铺名称：' }],
    postData:{},//请求数据
  },

  //选择位置
  chooseAddress:function(){
    var that = this;
    var data = that.data.postData;
    wx.chooseLocation({
      success: function (res) {
        console.log(res.name);
        console.log(res.address);
        data['merAddr'] = res.address;
        that.setData({
          postData: data,
        })
      },
    })
  },

  //输入框输入信息
  messageInput:function(e){
    var that = this;
    var data = that.data.postData;
    console.log(e.currentTarget.id);
    console.log(e.detail.value);
    if (e.currentTarget.id ==0){
      data['slogan'] = e.detail.value;
    } else if (e.currentTarget.id == 1) {
      data['contactName'] = e.detail.value;
    } else if (e.currentTarget.id == 2) {
      data['contactTel'] = e.detail.value;
    } else if (e.currentTarget.id == 3) {
      data['merName'] = e.detail.value;
    } else {
      data['merAddr'] = e.detail.value;
    }
    that.setData({
      postData: data,
    })
  },

  //选择主营
  chooseTimberType:function(){
    wx.navigateTo({
      url: '../timberType/timberType',
    })
  },

  saveClick:function(){
    var that = this;
    if (that.data.postData.contactTel.length != 11){
      wx.showToast({
        title: '手机号有误，请修改',
        icon:'none',
      })
      return;
    }
    app.post('card/saveOrUpdateMerCard', that.data.postData).then((res) => {
      console.log('success');
      console.log(res);
      if(res.code == 0){
        var userInfo = wx.getStorageSync('userInfo');
        userInfo['cardMsg'] = { 'slogan': that.data.postData.slogan, 'contactName': that.data.postData.contactName, 'contactTel': that.data.postData.contactTel, 'merName': that.data.postData.merName, 'merAddr': that.data.postData.merAddr, 'variety': that.data.postData.variety};
        userInfo['isCard']= 0;
        wx.setStorageSync('userInfo', userInfo);

        var pages = getCurrentPages();//获取页面栈
        var prevPage = pages[pages.length - 2];//上一页
          prevPage.setData({
            userInfo: wx.getStorageSync('userInfo'),
          })
        wx.navigateBack();
      }else{
        wx.showModal({
          title: '温馨提示',
          content: res.msg,
          showCancel: false,
        })
      }
    }).catch((errMsg) => {
      console.log('fail');
      console.log(errMsg + '失败');
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var data = that.data.postData;
    data['openId'] = wx.getStorageSync('userInfo').userId;
    data['username'] = wx.getStorageSync('userInfo').username;
    data['userIcon'] = wx.getStorageSync('userInfo').user_icon;
    if (wx.getStorageSync('userInfo').isCard == 0){
      data['slogan'] = wx.getStorageSync('userInfo').cardMsg.slogan;
      data['contactName'] = wx.getStorageSync('userInfo').cardMsg.contactName;
      data['contactTel'] = wx.getStorageSync('userInfo').cardMsg.contactTel;
      data['merName'] = wx.getStorageSync('userInfo').cardMsg.merName;
      data['merAddr'] = wx.getStorageSync('userInfo').cardMsg.merAddr;
      data['variety'] = wx.getStorageSync('userInfo').cardMsg.variety;
    }
    that.setData({
      postData:data,
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})